function setCookie(key, val, day, path = "/") {
    if (day) {  //如果设置了过期时间
        var date = new Date();  //北京时间(世界时 + 8h)
        date.setDate(date.getDate() + day);
        // 存储时 中文编码
        document.cookie = key + "=" + encodeURIComponent(val) + ";expires=" + date.toUTCString() + ";path=" + path;
    } else {
        document.cookie = key + "=" + encodeURIComponent(val) + ";path=" + path;
    }
}

function getCookie(key) {  //用户期望查询的键名 "user" "pwd"
    var cookie = document.cookie; // 取出所有cookie
    if (cookie) {
        var arr = cookie.split("; ");  //拆分解析
        for (var i = 0; i < arr.length; i++) {
            var item = arr[i];
            var subArr = item.split("=");
            var name = subArr[0];
            var val = subArr[1];
            // 获取时 中文解码
            if (key === name) { //如果用户传入的键名 和拆分键名相同  => 使我们想要的数据
                return decodeURIComponent(val);
            }
        }
    }
    return "";
}

function delCookie(key) {
    setCookie(key, "", -1);
}


